//
// Created by Liming Shao on 10/29/2017.
//

#include "E331.h"
#include <iostream>

void E331::spiralOrderPrint(std::vector<std::vector<int> > matrix) {
    int row = (int) matrix.size();
    int col = (int) matrix[0].size();

    for (int i = 0; col > 0 && row > 0 ; ++i) {
        printOuterCircle(matrix, i, row, col);
        col -= 2;
        row -= 2;
    }
}

void E331::printOuterCircle(std::vector<std::vector<int> > matrix, int n, int r, int c){
    if(r!=1 && c!=1){
        for (int i = 0; i < c-1; ++i) {
            std::cout << matrix[n][n+i] << " ";
        }
        for (int j = 0; j < r-1; ++j) {
            std::cout << matrix[n+j][n+c-1] << " ";
        }
        for (int k = 0; k < c-1; ++k) {
            std::cout << matrix[n+r-1][n+c-k-1] << " ";
        }
        for (int l = 0; l < r-1; ++l) {
            std::cout << matrix[n+r-1-l][n] << " ";
        }
    }else if(r==1){
        for (int i = 0; i < c; ++i) {
            std::cout << matrix[n][n+i] << " ";
        }
    }else{
        for (int i = 0; i < r ; ++i) {
            std::cout << matrix[n+i][n] << " ";
        }
    }
}

void E331::test() {
    int ma[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

    std::vector<std::vector<int> > matrix(5, std::vector<int>(6));
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 6; ++j) {
            matrix[i][j] = 6 * i + j + 1;
        }
    }

    spiralOrderPrint(matrix);
}
